package com.bidevalution.business.integrate;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.drools.compiler.lang.DRL5Expressions.expression_return;
import org.springframework.util.StringUtils;
import com.bidevalution.business.factory.StepJobFactory;
import com.bidevalution.business.factory.StepRowJobFactory;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.qtp.core.util.GsonUtil;
/**
 * 
 * @Description: 存储过程执行任务
 * @author daizhifeng   
 * @date 2019年9月11日 下午5:17:58 
 * @version V2.0
 */
public class ProcedureExecuteRowJob implements StepRowJobFactory{
	private JdbcTemplateService jdbcTemplateService;
	private Logger logger = Logger.getLogger(getClass());
	/**
	 * <p>Description: 执行每个环节配置在存储过程</p>
	 * @param jdbcTemplateService
	 * @see com.bidevalution.business.factory.StepJobJdbcFactory#setJdbcTemplateService(com.bidevalution.business.service.JdbcTemplateService)
	 */
	@Override
	public Boolean execute(BeSectionStepJob beSectionStepJob,String submitCode,String expertId,Map<String, Object> paramMap) throws Exception {
		String jobContent = beSectionStepJob.getJobContent();
		if(!StringUtils.isEmpty(jobContent)) {
			paramMap.put("$sectionStepId", beSectionStepJob.getSectionStepId());
			paramMap.put("$submitCode", submitCode);
			paramMap.put("$sectionId", beSectionStepJob.getSectionId());
			paramMap.put("$expertId", expertId);
			paramMap.put("$layoutCode", beSectionStepJob.getLayoutCode());
			if (jobContent.toLowerCase().startsWith("{ael[")) {
				jobContent = jobContent.substring(5, jobContent.length() - 2);
				logger.info("计算公式公式:" + jobContent);
				jobContent = FunctionCalculator.computeAviator(jobContent, paramMap);
			} else if (jobContent.toLowerCase().startsWith("{mel[")) {
				jobContent = jobContent.substring(5, jobContent.length() - 2);
				logger.info("计算公式公式:" + jobContent);
				jobContent = FunctionCalculator.computeMVEL(jobContent, paramMap);
			}
			for(String procedureParam:StringUtils.delimitedListToStringArray(jobContent, ";")) {
				//格式:p_c1|11,222;pc2|334,567345
				String[] produce = StringUtils.delimitedListToStringArray(procedureParam, "|");
				List<String> paramValues = Lists.newLinkedList();
				paramValues.add(beSectionStepJob.getSectionId());
				paramValues.add(beSectionStepJob.getSectionStepId());
				paramValues.add(submitCode);
				paramValues.add(beSectionStepJob.getLayoutCode());
				paramValues.add(expertId);
				if(produce.length == 2) {
					for(String paramValue:StringUtils.delimitedListToStringArray(produce[1], ",")) {
						if(!StringUtils.isEmpty(paramValue)) {
							paramValues.add(paramValue);
						}
					}
					logger.info("执行存储过程参数:"+produce[0]+",参数:"+paramValues);
					if(!this.jdbcTemplateService.executeProcedure(produce[0], paramValues.toArray())) {
						return false;
					}
				}else {
					if(!this.jdbcTemplateService.executeProcedure(produce[0], paramValues.toArray())) {
						return false;
					}
				}
				
				
			}
		}
		return true;
	}
	
	@Override
	public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
		this.jdbcTemplateService = jdbcTemplateService;
	}
}
